<html>
<head>
	<link rel="stylesheet" type="text/css" href="../fshl/styles/COHEN_style.css" media="all" />
</head>
<body>
<pre>
<?php

	// This example shows how to highlight very large files with limited memory resources.
	
	// NOTE: This script does not resolve time execution limitations.
	
	require_once('../fshl/fshl.php');
	
	// setup your large file here
	$file_name = '../fshl/fshl_cache/PHP_lang.php';
	// setup limited buffer size here
	$buffer_size = 1024;
	// parser options
	$output_module = 'HTML_UTF8';
	$start_language = 'PHP';
	$parser_options = P_TAB_INDENT | P_LINE_COUNTER;
	
	// create parser
	$parser = new fshlParser($output_module, $parser_options);
	
	$handle = @fopen($file_name, 'r');
	if ($handle) {
	
		$file_size = filesize($file_name);
		$file_pos = 0;
		$iteration = 0;
		$time = 0;
		$fragment = null;
		$out = null;
		$out_length = 0;
		
		// let's go
		
		while ($file_pos < $file_size) {
			$data = fread($handle, $buffer_size);
			$text = null;
			$file_pos += strlen($data);
			
			if ($file_pos < $file_size) {
				// To get fine results, we must protect text against breaking inside the delimiters.
				// For example, when the '/*' delimiter will be splited to '/' and '*', the parser
				// can't switch to correct state. The best way to hack this is find last line breaker
				// (because line breakers are used in all lexers) and split the input text.
				if ($pos = strrpos($data, "\n")) {
					$text = $fragment.substr($data, 0, $pos);
					$fragment = substr($data, $pos+1);
				} else {
					$text = $fragment.$data;
					$fragment = "";
				}
			} else {
				// at last, we only adds previous fragment to the chunk
				$text = $fragment.$data;
			}
						
			// let's do the trick
			if (!$iteration++) {
				// first iteration, couter padding is 5 (up to 99999)
				$out = $parser->highlightString($start_language, $text, 0, 5);
				$time = $parser->getParseTime();
			} else {
				// next iterations
				$out = $parser->highlightNextString($text);
				$time += $parser->getParseTime();
			}

			// NOTE: Results are sends directly to the output, but it's easy to modify script
			//       to saving results to the file.

			$out_length += strlen($out);
			echo $out;
		}
		fclose($handle);
		
		echo "\n\n\nFSHL Statistics:\n\n";
		printf("      total time: <b>%0.3f</b> sec (%0.2f kB/s)\n", $time, $out_length/$time/1024);
		echo "     file length: <b>${file_size}</b>\n";
		echo "   output length: <b>${out_length}</b>\n";
		echo "      iterations: <b>${iteration}</b>\n";
		
	} else {
		echo "\n\nFile <b>$file_name</b> not found\n\n";
	}
?>
</pre>
</body>
</html>